﻿Imports System
Imports System.Collections.Generic

Public Class DisposableArray
	Implements IDisposable

	#Region "Members"
		' To detect redundant calls.
		Private _isDisposed As Boolean = False
		Private _items      As IDisposable()
	#End Region

	#Region "Constructors"
		Public Sub New(ParamArray __items As IDisposable())
			_items = __items
		End Sub

		Public Sub New(__items As List(Of IDisposable))
			_items = __items.ToArray()
		End Sub
	#End Region

	#Region "Methods"
		Protected Overridable Sub Dispose(ByVal disposing As Boolean)
			If Not _isDisposed Then
				If disposing Then
					' TODO: free other state (managed objects).
					If _items IsNot Nothing Then
						For Each item As IDisposable In _items
							item.Dispose()
						Next
					End If
				End If

				' TODO: free your own state (unmanaged objects),
				'       and set large field to Nothing.
				_items = Nothing
			End If

			_isDisposed = True
		End Sub

		#Region " IDisposable Support "
			' This code added by Visual Basic to correctly implement the disposable pattern.
			Public Sub Dispose() Implements IDisposable.Dispose
				' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
				Dispose(True)
				GC.SuppressFinalize(Me)
			End Sub
		#End Region
	#End Region
End Class
